//
//  ATRegisterViewController.m
//  Adorable_Things
//
//  Created by ybq on 16/1/19.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ATRegisterViewController.h"
#import "BQNavViewCommon.h"
#import "Utils.h"
#import "ATEnterCodeViewController.h"

@interface ATRegisterViewController ()
{
    MBProgressHUD *HUD;
}
@property (nonatomic,strong) BQNavViewCommon *navView;

@end

@implementation ATRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
}

- (void)createUI {
    _navView = [[BQNavViewCommon alloc] initWithFrame:CGRectMake(0, 20, kWidth, 44)];
    [_navView.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _navView.titleLabel.text = _myTitle;
    [self.view addSubview:_navView];
    
    _backGroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h"]];
    _getSecurityCodeButton.layer.cornerRadius = 25;
}

- (IBAction)getSecurityCodeAction:(id)sender {
    if ([Utils checkTelNumber:_phoneNumberTF.text]) {
        
        if ([_myTitle isEqualToString:@"注册"]) {
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            parameters[@"phone"] = [NSString stringWithFormat:@"%@",_phoneNumberTF.text];
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            [manager POST:@"http://api.menghuoapp.com/v1/user/register_verify" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSString *result = responseObject[@"msg"];
                NSString *data = responseObject[@"data"];
                if ([result isEqualToString:@"手机号已注册"]) {
                    
                    [self createwaringAlert];
                    
                } else {
                    
                    ATEnterCodeViewController *enterVC = [[ATEnterCodeViewController alloc] init];
                    enterVC.verify_code = data;
                    enterVC.phone = _phoneNumberTF.text;
                    enterVC.myTitle = @"设置密码";
                    [self.navigationController pushViewController:enterVC animated:YES];
                    [self showHUDWithString:@"验证码已发送,请注意查收"];
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        } else {
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            parameters[@"phone"] = [NSString stringWithFormat:@"%@",_phoneNumberTF.text];
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            [manager POST:@"http://api.menghuoapp.com/v1/user/forget_pwd" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSString *result = responseObject[@"msg"];
                NSString *data = responseObject[@"data"];
                if ([result isEqualToString:@"OK"]) {
                    ATEnterCodeViewController *enterVC = [[ATEnterCodeViewController alloc] init];
                    enterVC.verify_code = data;
                    enterVC.phone = _phoneNumberTF.text;
                    enterVC.myTitle = @"重新设置密码";
                    [self.navigationController pushViewController:enterVC animated:YES];
                    [self showHUDWithString:@"验证码已发送,请注意查收"];
                }
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }
        
        
        
    } else {
        [self showHUDWithString:@"您输入的手机号码不正确"];
    }
}

#pragma mark --- HUD
- (void)showHUDWithString:(NSString *)string {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = string;
    HUD.mode = MBProgressHUDModeText;
    
    //指定距离中心点的X轴和Y轴的偏移量，如果不指定则在屏幕中间显示
    //    HUD.yOffset = 150.0f;
    //    HUD.xOffset = 100.0f;
    
    [HUD showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [HUD removeFromSuperview];
        HUD = nil;
    }];
}

- (void)createwaringAlert {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"阿萌提示" message:@"啊咧,这个手机号码已被注册= = |||" preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alertController animated:YES completion:nil];
    
    // 添加按钮
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:loginAction];
    alertController.preferredAction = loginAction;
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)backButtonAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
